<?php

// save content into a xml file, better with ATOM format
class Blog extends DomDocument {
    function __construct() {
        //has to be called!
        parent::__construct();
    }
    
	// add a new blog entry, automatcially generate its id and internal meta data
    function addEntry($title, $content) {
        $ts = $this->createElement("title");
        $tt = $this->createTextNode($title);
        $ts->appendChild($tt);
		
		$cs = $this->createElement("content");
		$ct = $this->createTextNode($content);
		$cs->appendChild($ct);
		
		$is = $this->createElement("id");
		$it = $this->createTextNode(md5(getmypid().uniqid(rand()).$_SERVER['SERVER_NAME']));
		$is->appendChild($it);
		
		$item = $this->createElement("entry");
		$item->appendChild($ts);
		$item->appendChild($cs);
		$item->appendChild($is);
        $this->documentElement->appendChild($item);
    }
	
	// get a short list for displaying recent blog entry titles
	function getShortList($num=10){
		$first = $num;
		
		$xp = new DomXPath($this);
		$xp->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
		$res = $xp->query("//atom:entry");
		$list = "";
		foreach ($res as $node) {
	   		$list =  $first. "#&nbsp; <a href='#' onclick='loadEntry(\"".$node->getElementsByTagName("id")->item(0)->textContent ."\");return false;'>".$node->getElementsByTagName("title")->item(0)->textContent . "</a><br/>". $list;
			//break;
			$first--;
			if($first==0) break;
		}

		return $list;
	}
	
	// get a blog entry by its id
	function loadEntry($id){
		
		//$id = 'faf51486fd6b518fb630b01a65651071';
		$xp = new DomXPath($this);
		$xp->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
		$res = $xp->query("//atom:entry[atom:id= '" . $id . "']");
		foreach ($res as $node) {
    		$content = $node->getElementsByTagName("content")->item(0)->textContent;
			//break;
		}
		//$entry = $res->item[0];
		//$content = $entry->textContent;
		return $content;
	}
	
	//update an entry by its id with new title and content
	function updateEntry($id, $title, $content){
		//$this->setElementByPath("//atom:entry[id='".$id."']/content","atom", 'http://www.w3.org/2005/Atom', $content);
		$xp = new DomXPath($this);
		$xp->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
		$res = $xp->query("//atom:entry[atom:id= '" . $id . "']");
		foreach ($res as $node) {
    		$node->getElementsByTagName("content")->item(0)->nodeValue=$content;
			$node->getElementsByTagName("title")->item(0)->nodeValue = $title;
			//break;
		}
	}
	
	// get a blog entry by it's id as well it's revision
	function loadEntryByRevision($id,$rev){
		// take revision information or not based on user's preference
		// or save to another file for storage integrity
		
	}
	
	function debug($x){
		$fp = fopen ( "debug.txt" , "w+" ) or die ("Cannot open $file for writing, check permissions");
        fwrite ( $fp, $x );
        fclose ( $fp );
	
	}
	
	
}


?>